﻿using DAL.Entities;
using Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.RYGL
{
    public class EmployeeDAL
    {
        private readonly DataContext _dataContext;

        public EmployeeDAL(DataContext dataContext)
        {
            _dataContext = dataContext;
        }

        public Task<IEnumerable<Employee>> ListAsync(string deptid, string name, string employeeType, string inoutType, int pageSize, int pageIndex, out int total)
        {
            var query = from q in _dataContext.Employees.AsNoTracking()
                        select q;

            if (!string.IsNullOrEmpty(employeeType)) query = query.Where(x => x.EmployeeType == employeeType);
            if (!string.IsNullOrEmpty(deptid)) query = query.Where(x => x.DepartmentId == deptid);
            if (!string.IsNullOrEmpty(name)) query = query.Where(x => x.DisplayName.Contains(name));
            if (!string.IsNullOrEmpty(inoutType))
            {
                if (inoutType == "已退场")
                    query = query.Where(x => x.EmployeeStatus == inoutType);
                else
                    query = query.Where(x => x.EmployeeStatus != "已退场");
            }

            total = query.CountAsync().Result;
            var data = query.OrderByDescending(x => x.CreateTime).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
            foreach (var item in data)
            {
                if (!string.IsNullOrEmpty(item.DepartmentId))
                {
                    var dept = _dataContext.Database.SqlQuery<UnitModel>("select * from Unit where UnitId = '" + item.DepartmentId + "'").ToList().FirstOrDefault();
                    if (dept != null)
                    {
                        item.DepartmentName = dept.UnitName;
                    }
                }
            }
            return Task.FromResult(data as IEnumerable<Employee>);
        }

        public async Task<Employee> GetAsync(Guid id)
        {
            return await _dataContext.Employees.FindAsync(id);
        }

        public async Task CreateAsync(Employee entity)
        {
            _dataContext.Employees.Add(entity);
        }

        public async Task UpateAsync(Employee entity)
        {
            _dataContext.Entry(entity).State = EntityState.Modified;
            //需要存库
        }

        public async Task DeleteAsync(Employee entity)
        {
            _dataContext.Employees.Remove(entity);
        }

        public async Task SaveChangesAsync()
        {
            await _dataContext.SaveChangesAsync();
        }

        public async Task<int> CountAsync(string deptid, string employeeType, DateTime? date)
        {
            var query = from q in _dataContext.Employees
                        select q;

            if (!string.IsNullOrEmpty(deptid)) query = query.Where(x => x.DepartmentId == deptid);
            if (!string.IsNullOrEmpty(employeeType)) query = query.Where(x => x.EmployeeType == employeeType);
            if (date != null)
            {
                var from = date.Value.Date;
                var to = date.Value.Date.AddDays(1);
                query = query.Where(x => x.ReworkDate > from && x.ReworkDate < to);
            }

            return await query.CountAsync();
        }

        public async Task<int> Count2Async(string deptid, string employeeType)
        {
            var query = from q in _dataContext.Employees
                        where q.EmployeeStatus != "已退场"
                        select q;

            if (!string.IsNullOrEmpty(deptid)) query = query.Where(x => x.DepartmentId == deptid);
            if (!string.IsNullOrEmpty(employeeType)) query = query.Where(x => x.EmployeeType == employeeType);

            return await query.CountAsync();
        }

        public async Task<bool> Exists(string deptid, string identityNumber, Guid? id = null)
        {
            var query = from q in _dataContext.Employees
                        where q.DepartmentId == deptid && q.IdentityNumber == identityNumber
                        select q;

            if (id != null) query = query.Where(x => x.EmployeeId != id);

            var total = await query.CountAsync();
            return total > 0;
        }
    }
}
